package com.bugstack.domain.strategy.service.rule.tree.impl;

import com.bugstack.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import com.bugstack.domain.strategy.service.rule.tree.ILogicTree;
import com.bugstack.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.bugstack.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: czb
 * @Description: 幸运(兜底)奖规则树节点
 * @DateTime: 2025/3/10 16:03
 **/
@Slf4j
@Component("rule_luck_award")
public class RuleLuckAwardTreeNode implements ILogicTree {
    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue, Date endDateTime) {
        log.info("规则过滤-幸运奖 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);
        if(ruleValue == null || ruleValue.isEmpty()){
            log.error("规则过滤-幸运奖-兜底奖品未配置 strategyId:{} awardId:{} ruleValue:{}", strategyId, awardId, ruleValue);
            throw new RuntimeException("兜底奖励配置错误:" + ruleValue);
        }

        String[] ruleValues = ruleValue.split(Constants.COLON);
        Integer luckAward = Integer.valueOf(ruleValues[0]);
        String luckAwardRuleValue = ruleValues.length > 1 ? ruleValues[1] : "";

        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .strategyAwardVO(DefaultTreeFactory.StrategyAwardVO.builder()
                        .awardId(luckAward)
                        .awardRuleValue(luckAwardRuleValue)
                        .build())
                .build();
    }
}
